package com.shujia.scala

import scala.io.Source

//元组
//val tuple: (Int, Int) = (1, 2)
//println(tuple._1)//通过下标获取数据
//println(tuple._2)
object Demo16SUmScore {
  def main(args: Array[String]): Unit = {
    //1、读取文件
    val scores: List[String] = Source.fromFile("data/score.txt").getLines().toList

    //2、解析数据取出学号和分数
    val idAndScore: List[(String, Int)] = scores
      .map(score => {
        val split: Array[String] = score.split(",")
        val id: String = split(0)
        val sco: Int = split(2).toInt
        //返回一个二元组
        (id, sco)
      })

    //3、按照学号分组
    val groupByList: Map[String, List[(String, Int)]] = idAndScore
      .groupBy(kv => kv._1)

    //4、计算总分
    groupByList
      .mapValues(value => {
        //取出分数
        val sco: List[Int] = value.map(kv => kv._2)
        //分数求和
        val sumSco: Int = sco.sum
        sumSco
      })
      .foreach(println)
  }
}
